﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PolicyMainUpdate : System.Web.UI.Page
    {
        private int editid = 0, delid = 0, page = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterRole"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (!IsPostBack)
            {
                if (delid > 0)
                {
                    try
                    {
                        TestPaperPolicySub.DeleteByMainPolicyId(delid);
                        TestPaperPolicyMain.Delete(delid);
                        Response.Redirect("PolicyMain.aspx?page=" + page);
                    }
                    catch
                    { 
                    
                    }
                    return;
                }
                BindSubject();
                if (editid > 0)
                {
                    try
                    {
                        TestPaperPolicyMainInfo main = TestPaperPolicyMain.GetT1(editid);
                        this.tbName.Text = main.Name;
                        if (main.SubjectId > 0)
                        {
                            this.drpSubject.SelectedValue = main.SubjectId.ToString();
                        }
                    }
                    catch
                    { 
                    
                    }
                }
            }
        }

        void BindSubject()
        {
            IList<SubjectInfo> list = Subject.GetSubjectInfos();
            this.drpSubject.DataTextField = "Name";
            this.drpSubject.DataValueField = "Id";
            this.drpSubject.DataSource = list;
            this.drpSubject.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("请填写名称", this);
                return;
            }
            else
            {
                if (this.drpSubject.SelectedIndex==-1)
                {
                    Utility.ShowJsAlert("请选择科目", this);
                    return;
                }

                TestPaperPolicyMainInfo main = new TestPaperPolicyMainInfo();
                main.Name = this.tbName.Text;
                main.SubjectId = int.Parse(this.drpSubject.SelectedValue);
                if (editid > 0)
                {
                    main.Id = editid;
                    TestPaperPolicyMain.Edit(main);
                    Utility.ReloadTopPage(this);
                }
                else
                {
                    TestPaperPolicyMain.Add(main);
                    Utility.ReloadTopPage(this);
                }
            }
        }
    }
}
